package com.ksr.logistics.service.impl;

import com.github.pagehelper.PageHelper;
import com.ksr.logistics.mapper.RoleMapper;
import com.ksr.logistics.pojo.*;
import com.ksr.logistics.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> selRoles() {
        RoleExample example=new RoleExample();
        return roleMapper.selectByExample(example);
    }

    @Override
    public List<Role> selPageRoles(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer pageSize, String keyWord) {
        RoleExample example=new RoleExample();
        PageHelper.startPage(page,pageSize);
        if (StringUtils.isNotBlank(keyWord)){
            RoleExample.Criteria criteria = example.createCriteria();
            criteria.andRolenameLike("%"+keyWord+"%");
        }
        List<Role> roles = roleMapper.selectByExample(example);
        return roles;
    }

    @Override
    public int deleteById(Integer id) {
        return roleMapper.deleteByPrimaryKey((long)id);
    }


    @Override
    public Role selById(Integer id) {
        return roleMapper.selectByPrimaryKey((long)id);
    }

    @Override
    public int insertRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public int updateRole(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public List<Role> selByRoleName(String roleName) {
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andRolenameEqualTo(roleName);
        return roleMapper.selectByExample(example);
    }
}
